package num692;

import java.util.*;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/9 2:17
 * @Desc :
 */
public class Solution {
  public List<String> topKFrequent(String[] words, int k) {
    HashMap<String, Integer> map = new HashMap<>();
    for (String word : words) {
      Integer freq = map.getOrDefault(word, 0);
      map.put(word, freq + 1);
    }
    ArrayList<String> list = new ArrayList<>(map.keySet());
    list.sort(((o1, o2) -> {
      Integer i1 = map.get(o1);
      Integer i2 = map.get(o2);
      if (!i1.equals(i2)) return i2 - i1;
      else return o1.compareTo(o2);
    }));
    return list.subList(0, k);
  }

  public static void main(String[] args) {
    List<String> res = new Solution().topKFrequent(new String[]{"i", "love", "leetcode", "i", "love", "coding"}, 2);
    System.out.println(res);

    List<Integer> list = new ArrayList<>(List.of(1, 2, 3, 4, 5));
    list.sort((o1, o2) -> {
      return o1 - o2;
    });
    System.out.println(list);
  }
}
